package com.cl.short_video_java.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cl.short_video_java.pojo.VideoShopOrder;
import com.cl.short_video_java.service.VideoShopOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author 付
 * @Date 2024-11-16 17:00
 * @Version 1.0
 * @description
 * @since 1.0
 */

@Component
@Slf4j
public class AliPayTask {

	@Autowired
	private VideoShopOrderService orderService;


	/**
	 * 30秒查询一次
	 */
//	@Scheduled(cron = "0 0/3 * * * ?")
	@Scheduled(cron = "0 0/10  * * * ?")
	public void orderConfirm() {
		log.info("订单是否付款轮询====================================");
		LocalDateTime now = LocalDateTime.now();
		LocalDateTime minus = now.minus(Duration.ofMinutes(2));
		// 查询超过10分钟未支付的订单
		List<VideoShopOrder> list = orderService.list(new QueryWrapper<VideoShopOrder>().eq("status", 0).ge("create_time", minus));
		if (list.size() <= 0) {
			return;
		}
		for (VideoShopOrder order : list) {
			orderService.checkOrderStatus(order.getOrderId());
		}
	}

	@Scheduled(cron = "0/1 15  * * * ?")
	public void orderCancel() {
		log.info("订单超时定时任务执行====================================");
		LocalDateTime now = LocalDateTime.now();
		LocalDateTime minus = now.minus(Duration.ofMinutes(10));
		// 查询超过10分钟未支付的订单
		List<VideoShopOrder> list = orderService.list(new QueryWrapper<VideoShopOrder>().eq("status", 0).le("create_time", minus));
		if (list.size() <= 0) {
			return;
		}
		for (VideoShopOrder order : list) {
			order.setIsDelete(1);
		}
		orderService.updateBatchById(list);
	}

}
